define(["jquery","bbcare","as/app/assessComm/assessComm","as/app/cnbsr/cnbsrDetail.servers","as/app/cnbsr/cnbsrDetail.ui"],function($,$bb,$assessComm,$servers,$ui){
	var cnbsrDetail = {};

	/**
	 * 提交成功后的回调函数
	 */
	var ajaxSubmitCallBack = function(data){
		if( $(".cnbsr-wrap").parents(".assess-wrap").length == 0 ){
			$bb.openPage('cnbsrHistoryRecord',data);
		}else{
			$.get("page/assess/html/cnbsr/cnbsrHistoryRecord.html",function(html){
				$(".assess-wrap").html(html);
			});
		}
	}

	/**
	 * 提交答案
	 */
	var onSubmit = function(){
		var data = $(this).data("params");
		if( data.result.length == 0 ){
			$bb.toastShow("请先做评估问卷，再点击提交！");
		}else{
			var channelId = $bb.getChannelId();
			var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
			var postionIds = JSON.stringify(data.result);
			var params = {
				"oprInfo":{
					"opCode":"1127",
					"opType":"0",
					"opNote":"【"+ doctorName +"】医生为【"+ data.info.userName +"】患者上传儿心评估数据",
					"channelType":"2",
					"channelId":channelId,
				},
				"evaluationType":data.info.evaluationType,
				"queueUserId":data.info.userId,
				"postionIds":postionIds,
			};
			if( $(".cnbsr-wrap").parents(".assess-wrap").length > 0 ){
				var taskId = $(".cnbsr-wrap").parents(".assess-wrap").data("ptTaskId");
				$.extend(params,{"taskId":taskId});
			}
			$assessComm.servers.ajaxSubmit(params,ajaxSubmitCallBack);
		}
	}

	/**
	 * 选择答案
	 */
	var onAnswerSelect = function(){
		var params = $(this).data("params");
		var result = params.result;
		var id = $(this).attr("id");
		if( $(this).hasClass("active") ){
			$(this).removeClass("active");
			for (var i = 0; i < result.length; i++) {
				if( result[i] == id ) result.splice(i,1);
			}
		}else{
			$(this).addClass("active");
			result.push(id);
		}
		$("#cnbsrSubmit").unbind("click");
		$("#cnbsrSubmit").data("params",params);
		$("#cnbsrSubmit").click(onSubmit);
	}

	/**
	 * 选择x月智龄
	 */
	var onTabSelect = function(){
		var params = $(this).data("params");
		var index = $(this).attr("data-index");
		$(this).addClass("active").siblings().removeClass("active");
		var list = $servers.handleShowQuestions(params.questions,index);
		var handleQuestions = $servers.formatQuestion(list,index);
		var questValue = $servers.mixtureQuestValue(handleQuestions.content,params.result);
		$ui.content({
			"id":"cnbsrContent",
			"title":handleQuestions.title,
			"content":questValue
		});
		if( !params.info.status ){
			$("#cnbsrContent div.cnbsr-answer").css({"cursor":"pointer"});
			$("#cnbsrContent div.cnbsr-answer").unbind("click");
			$("#cnbsrContent div.cnbsr-answer").data("params",params);
			$("#cnbsrContent div.cnbsr-answer").click(onAnswerSelect);
		}
		$("#cnbsrSubmit").unbind("click");
		$("#cnbsrSubmit").data("params",params);
		$("#cnbsrSubmit").click(onSubmit);
	}

	/**
	 * 翻页
	 */
	var onPageSelect = function(){
		var params = $(this).data("params");
		var group = $(this).attr("group");
		if( group == "prev" && params.page > 0 ){
			params.page--;
			uiTab(params);
		}
		if( group == "next" && params.page < Math.ceil(params.questions.length / 8) - 1 ){
			params.page++;
			uiTab(params);
		}
	}

	/**
	 * 渲染tab
	 */
	var uiTab = function(params){
		var list = $servers.showTabQuestions(params.questions,params.page);
		$ui.tab({
			"id":"cnbsrTab",
			"list":list
		});
		// 选择x月智龄
		$("#cnbsrTab li[data-index]").unbind("click");
		$("#cnbsrTab li[data-index]").data("params",params);
		$("#cnbsrTab li[data-index]").click(onTabSelect);
		$("#cnbsrTab li[data-index]").eq(0).click();
		// 上一页、下一页
		$("#cnbsrTab li[group]").unbind("click");
		$("#cnbsrTab li[group]").data("params",params);
		$("#cnbsrTab li[group]").click(onPageSelect);
	}

	/**
	 * 查询评估量表问卷成功后的回调函数
	 */
	var ajaxPostionCallBack = function(data,params){
		var questions = $servers.handleQuestions(data);
		var page = 0;
		var result = params.status == "01" ? JSON.parse(params.result) : [];
		var arg = {
			"info":params,"questions":questions,"page":page,"result":result
		};
		uiTab(arg);
	}

	/**
	 * 渲染基本信息
	 */
	var uiInfo = function(data,params){
		var info = $servers.handleInfo(data);
		$ui.info({
			"id":"cnbsrInfo",
			"data":info
		});
	};

	cnbsrDetail.init = function(){
		var params = $bb.getPageParams();
		if( !params.evaluationType ) params.evaluationType = "Neuropsychology";
		// $(".cnbsr-wrap").parents(".assess-wrap").length 判断是从患者中心打开 还是从cnbsr菜单打开
		if( $(".cnbsr-wrap").parents(".assess-wrap").length > 0 ){
			$("#cnbsrInfoWrap").hide();
			$("#nowTime").parent().css("display","none");
		}else{
			$assessComm.servers.ajaxUserMassage(params,uiInfo);
		}
		$assessComm.servers.ajaxPostion(params,ajaxPostionCallBack);
		// 检查时间 检查医生
		var doctorName, testTime;
		if( params.status == "01" ){
			doctorName = params.doctorName;
			testTime = params.testTime;
			$("#cnbsrSubmit").css({"display":"none"});
		}else{
			doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
			testTime = $bb.simple.getCurTime().date;
		}
		$("#userName").html(doctorName);
		$("#nowTime").html(testTime);
	}

	return cnbsrDetail;
});